//
//  WmcFlashBtn.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/15.
//

import UIKit
import YPGraphicEngine

extension YPGECamera.FlashMode {
    /// 闪光灯模式
    struct Info {
        var title: String
        var image: UIImage
    }

    /// 信息
    var info: Info {
        switch self {
        case .off: return .init(title: "关闭", image: "home_menu_flash_off".wmc_image)
        case .auto: return .init(title: "自动", image: "home_menu_flash_auto".wmc_image)
        case .on: return .init(title: "打开", image: "home_menu_flash_on".wmc_image)
        case .torch: return .init(title: "常亮", image: "home_menu_flash_torch".wmc_image)
        }
    }
}

// MARK: - [WmcFlashBtn]

class WmcFlashBtn: WmcTopImgBottomTextBtn {
    /// 事件Block
    var actionBlock: YPBlock.Action<WmcFlashBtn>?
    /// 正常颜色
    var normalColor: UIColor = .white
    /// 选中颜色
    var selectColor: UIColor = .wmc_mainTheme

    /// 闪光灯类型
    var flashMode: YPGECamera.FlashMode = .off {
        didSet {
            self.title = flashMode.info.title
            self.imageView.image = flashMode.info.image
            if flashMode == .off {
                self.imageView.yp.set(tintColor: normalColor)
                self.label.textColor = normalColor
            } else {
                self.imageView.yp.set(tintColor: selectColor)
                self.label.textColor = selectColor
            }
        }
    }

    static func Create() -> WmcFlashBtn {
        let btn = WmcFlashBtn()
        btn.font = 12.wmc_font
        btn.imgTextSpacing = 2
        btn.imgSize = 24
        btn.yp.addEvent { btn in
            YPDevice.PlayShake(.heavy)
//            btn.clickFlash()
            btn.actionBlock?(btn)
        }
        btn.flashMode = .off
        return btn
    }

    /// 递增事件
    func clickFlash() {
        let new = flashMode.rawValue
        if let newCase = YPGECamera.FlashMode(rawValue: new + 1) {
            flashMode = newCase
        } else {
            flashMode = .off
        }
    }
}
